package com.zhuge_new.Untils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fan on 2016/12/5.
 */

public class DataParse {
    /**Json数据存入实体类
     *
     * @param json String
     * @param c Class 相应的实体类
     * @param type 1代表传来的json类型是JSONObject
     * */
    public Object getBean(String json, final Class c, int type){
        Object obj = null;
        Gson gson=new Gson();
        obj=gson.fromJson(json, c);
        return obj;
    }

    public <T>List<T> getBean(String json, Class<T> c){
        List<T> list =new ArrayList<T>();
        Gson gson=new Gson();
        JsonArray jsonArray=new JsonParser().parse(json).getAsJsonArray();
        for(JsonElement jsonElement:jsonArray){
            list.add(gson.fromJson(jsonElement,c));
        }
        return list;
    }

}
